package com.jf.config;

import com.jf.model._MappingKit;
import com.jf.pay.job.QuartzManager;
import com.jf.routes.AdminRoutes;
import com.jf.routes.TestRoutes;
import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class JConfig extends JFinalConfig {
    @Override
    public void configConstant(Constants constants) {
        //SqlReporter.setLogger(true);
//        constants.setErrorView(401, "401.html");
//        constants.setErrorView(403, "403.html");
//        constants.setError404View("404.html");
//        constants.setError500View("500.html");
        constants.setEncoding("utf-8");
        // 加载数据库配置文
        loadPropertyFile("db.properties");
        constants.setDevMode(true);
        constants.setViewType(ViewType.JSP);
        constants.setMaxPostSize(524288000);
        constants.setViewType(ViewType.FREE_MARKER);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add(new AdminRoutes());
        routes.add(new TestRoutes());
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {
        DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),
                getProperty("user"), getProperty("password"));
        plugins.add(dp);

//        plugins.add(new EhCachePlugin());



        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(dp);
        plugins.add(activeRecordPlugin);
//        activeRecordPlugin.addMapping("user","id", User.class);

        activeRecordPlugin.setShowSql(true);
        activeRecordPlugin.setDialect(new MysqlDialect());// 配置MySql方言
        _MappingKit.mapping(activeRecordPlugin);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {
        handlers.add(new ContextPathHandler("base"));
    }

    // 启动时
    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();
        QuartzManager qm = new QuartzManager();
        qm.initJob();
    }

    // 销毁
    @Override
    public void beforeJFinalStop() {
        super.beforeJFinalStop();
//关闭定时任务管理器
        QuartzManager qm = new QuartzManager();
        qm.shutdown();
    }
}
